import service from '@/utils/request'

export const getFiles = (params) => {
  return service.get('/files', { params: { ...params } })
}

export const getFile = (id) => {
  return service.get(`/files/${id}`)
}

export const createFile = (data) => {
  return service.post('/files', data)
}

export const uploadFile = (data) => {
  return service.post('/files/upload', data)
}

export const downloadFile = (id) => {
  return service.get(`/files/download/${id}`, { responseType: 'blob' })
}

export const editFile = (id, callback) => {
  const editorWindow = window.open(`/editor?file=${id}`)

  // 使用 setInterval 定期检查窗口是否关闭
  const checkWindow = setInterval(() => {
    if (editorWindow.closed) {
      clearInterval(checkWindow)
      callback()
    }
  }, 1000)
}

export const moveToRecycleBin = (id) => {
  return service.delete(`/files/${id}`)
}

export const restoreFile = (id) => {
  return service.put(`/files/restore/${id}`)
}

export const deleteFile = (id) => {
  return service.delete(`/files/deleteCompletely/${id}`)
}
